package com.tanhua.manage.controller;


import cn.hutool.core.date.DateUtil;
import com.tanhua.manage.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    @GetMapping("summary")
    public Map getActiveInfo(){
        Map activeInfo = null;
        try {
            activeInfo = dashboardService.getActiveInfo();
            if (null == activeInfo){
                activeInfo.put("errCode","000021");
                activeInfo.put("errMessage","查询活跃信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activeInfo;
    }

    @GetMapping("users")
    public Map getUsersInfo(@RequestParam Long sd,
                            @RequestParam Long ed,
                            @RequestParam Integer type){
        //将时间戳转换成日期
        Date sdDate = new Date(sd);
        Date edDate = new Date(ed);

        //根据日期获取开始结束的所在的年份跟月份
        Integer sdMonth = DateUtil.month(sdDate) + 1;
        Integer edMonth = DateUtil.month(edDate) + 1;
        Integer sdYear = DateUtil.year(sdDate);
        Integer edYear = DateUtil.year(edDate);
        Date sdTime = null;
        Date edTime = null;
        try {
            sdTime = new SimpleDateFormat("yyyy-MM").parse(sdYear+"-"+sdMonth);//定义起始日期
            edTime = new SimpleDateFormat("yyyy-MM").parse(edYear+"-"+edMonth);//定义结束日期
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //求出结束的时间,用来做while的判断条件(请求截止月份的后一个月)
        Date edMonthAfterTime = getNextDate(edTime);

        Map usersInfo = null;

        try {
            usersInfo = dashboardService.getNewUsersInfo(sd,ed,sdTime,edTime,edMonthAfterTime,type);
            if (null == usersInfo){
                usersInfo.put("errCode","000022");
                usersInfo.put("errMessage","查询用户信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return usersInfo;
    }

    @GetMapping("distribution")
    public Map getDistributionInfo(@RequestParam Long sd, @RequestParam Long ed){
       //将时间戳转换成日期
        Date sdDate = new Date(sd);
        Date edDate = new Date(ed);

        //根据日期获取开始结束的所在的年份跟月份
        Integer sdMonth = DateUtil.month(sdDate) + 1;
        Integer edMonth = DateUtil.month(edDate) + 1;
        Integer sdYear = DateUtil.year(sdDate);
        Integer edYear = DateUtil.year(edDate);
        Date sdTime = null;
        Date edTime = null;
        try {
            sdTime = new SimpleDateFormat("yyyy-MM").parse(sdYear+"-"+sdMonth);//定义起始日期
            edTime = new SimpleDateFormat("yyyy-MM").parse(edYear+"-"+edMonth);//定义结束日期
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //求出结束的时间,用来做while的判断条件(请求截止月份的后一个月)
        Date edMonthAfterTime = getNextDate(edTime);

        Map distributionInfo = null;

        try {
            distributionInfo = dashboardService.getDistributionInfo(sd,ed,sdTime,edTime,edMonthAfterTime);

            if (null == distributionInfo){
                distributionInfo.put("errCode","000023");
                distributionInfo.put("errMessage","查询用户信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return distributionInfo;
    }


    //获取下一个月份
    public Date getNextDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }
}
